package com.baylor.server.business.util;

public class GeoTools {
    /**
     * Calculates geodetic distance between two points specified by latitude/longitude using Vincenty inverse formula
     * for ellipsoids
     *
     * @param lat1 first point latitude in decimal degrees
     * @param lon1 first point longitude in decimal degrees
     * @param lat2 second point latitude in decimal degrees
     * @param lon2 second point longitude in decimal degrees
     * @returns distance in meters between points with 5.10<sup>-4</sup> precision
     * @see <a href="http://www.movable-type.co.uk/scripts/latlong-vincenty.html">Originally posted here</a>
     */
    public static double distVincenty(double lat1, double lon1, double lat2, double lon2) {
        double a = 6378137, b = 6356752.314245, f = 1 / 298.257223563; // WGS-84 ellipsoid params
        double L = Math.toRadians(lon2 - lon1);
        double U1 = Math.atan((1 - f) * Math.tan(Math.toRadians(lat1)));
        double U2 = Math.atan((1 - f) * Math.tan(Math.toRadians(lat2)));
        double sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
        double sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);

        double sinLambda, cosLambda, sinSigma, cosSigma, sigma, sinAlpha, cosSqAlpha, cos2SigmaM;
        double lambda = L, lambdaP, iterLimit = 100;
        do {
            sinLambda = Math.sin(lambda);
            cosLambda = Math.cos(lambda);
            sinSigma = Math.sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda)
                    + (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda) * (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
            if (sinSigma == 0)
                return 0; // co-incident points
            cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
            sigma = Math.atan2(sinSigma, cosSigma);
            sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
            cosSqAlpha = 1 - sinAlpha * sinAlpha;
            cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
            if (Double.isNaN(cos2SigmaM))
                cos2SigmaM = 0; // equatorial line: cosSqAlpha=0 (§6)
            double C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
            lambdaP = lambda;
            lambda = L + (1 - C) * f * sinAlpha
                    * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
        } while (Math.abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0);

        if (iterLimit == 0)
            return Double.NaN; // formula failed to converge

        double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
        double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
        double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
        double deltaSigma = B
                * sinSigma
                * (cos2SigmaM + B
                / 4
                * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM
                * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
        double dist = b * A * (sigma - deltaSigma);

        return dist;
    }

    /**
     * <p>http://stackoverflow.com/questions/9457988/bearing-from-one-coordinate-to-another
     * <p>http://www.movable-type.co.uk/scripts/latlong.html
     *
     * @param lat1 latitude of start
     * @param lon1 longitude of start
     * @param lat2 latitude of end
     * @param lon2 longitude of end
     * @return
     */
    public static double bearing(double lat1, double lon1, double lat2, double lon2) {
        double longitude1 = lon1;
        double longitude2 = lon2;
        double latitude1 = Math.toRadians(lat1);
        double latitude2 = Math.toRadians(lat2);
        double longDiff = Math.toRadians(longitude2 - longitude1);
        double y = Math.sin(longDiff) * Math.cos(latitude2);
        double x = Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(longDiff);

        return (Math.toDegrees(Math.atan2(y, x)) + 360) % 360;
    }

    /**
     * bearing to direction string in simple Chinese
     *
     * @param bearing [0, 360)
     * @return
     */
    public static String bearing2str_zhCN(double bearing) {
        if ((bearing >= 0 && bearing < 22.5) || (bearing >= 337.5 && bearing < 360)) {
            return "北";
        } else if (bearing >= 22.5 && bearing < 67.5) {
            return "东北";
        } else if (bearing >= 67.5 && bearing < 112.5) {
            return "东";
        } else if (bearing >= 112.5 && bearing < 157.5) {
            return "东南";
        } else if (bearing >= 157.5 && bearing < 202.5) {
            return "南";
        } else if (bearing >= 202.5 && bearing < 247.5) {
            return "西南";
        } else if (bearing >= 247.5 && bearing < 292.5) {
            return "西";
        } else if (bearing >= 292.5 && bearing < 337.5) {
            return "西北";
        } else {
            return "输入值错误";
        }
    }

    /**
     * convert distance to more user friendly string in simple Chinese
     *
     * @param distance in meter
     * @return
     */
    public static String distance2str_zhCN(double distance) {
        int dist = (int) distance;
        if (dist < 1000) {
            return dist + "米";
        }
        if (dist < 10000) {
            return String.format("%.1f公里", dist / 1000.0);
        }
        if (dist < 100000) {
            return (dist / 1000) + "公里";
        }
        return String.format("%,3d公里", dist / 1000);
    }

    public static void main(String[] args) {
        System.out.println(distVincenty(31.23d, 121.43d, 31.239d, 121.439d));
        System.out.println(distance2str_zhCN(1315.6405546512988d));
    }
}
